/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * Configuration.java
 *
 * 配置文件支持类
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.core;

import java.io.FileInputStream;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Configuration {
	private static Element root;

	static {
		SAXReader reader = new SAXReader();
		FileInputStream fis = null;
		try {
			String path = Configuration.class.getResource("/").getPath()
					+ "config.xml";
			if (path.startsWith("/"))
				path = path.substring(1);
			fis = new FileInputStream(path);
			Document doc = reader.read(fis);
			root = doc.getRootElement();
			Common.managerEmail = getProperty("/config/manager-email", "tufeipin@gmail.com");
			Common.copyright = getProperty("/config/copyright");
		} catch (Exception e) {

		} finally {
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	public static String getProperty(String path) {
		return getProperty(path, "");
	}

	public static String getProperty(String path, String defaultValue) {
		Element el = (Element) root.selectSingleNode(path);
		if (el == null)
			return defaultValue;
		return el.getText();
	}
}
